﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace Happy.Utils
{
    /// <summary>
    /// 表达式工具。
    /// </summary>
    public static class ExpressionsUtil
    {
        /// <summary>
        /// 创建属性表达式。
        /// </summary>
        public static Expression<Func<T, TProperty>> CreatePropertyExpression<T, TProperty>(string property)
        {
            var parameter = Expression.Parameter(typeof(T));

            return Expression.Lambda<Func<T, TProperty>>(Expression.Property(parameter, property), parameter);
        }

        /// <summary>
        /// 获取属性表达式的属性名称。
        /// </summary>
        public static string GetPropertyName<T, TProperty>(Expression<Func<T, TProperty>> expression)
        {
            return (expression.Body as MemberExpression).Member.Name;
        }
    }
}